package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {

        /**
         * 插入订单数据
         * @param order
         */
        void insert(Orders order);

        /**
         * 根据订单号和用户id查询订单
         * @param orderNumber
         * @param userId
         */
        @Select("select * from sky_take_out.orders where number = #{orderNumber} and user_id= #{userId}")
        Orders getByNumberAndUserId(String orderNumber, Long userId);

        /**
         * 修改订单信息
         * @param orders
         */
        void update(Orders orders);

        /**
         * 用于替换微信支付更新数据库状态的问题
         *
         * @param orderStatus
         * @param orderPaidStatus
         */
        @Update("update sky_take_out.orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} where number = #{number}")
        void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, String number);


        /**
         * 分页条件查询并按下单时间排序
         * @param ordersPageQueryDTO
         */
        Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);


        /**
         * 根据id查询订单
         * @param id
         */
        @Select("select * from sky_take_out.orders where id=#{id}")
        Orders getById(Long id);

        /**
         * 根据订单id查询订单详情
         * @param id
         */
        @Select("select * from sky_take_out.order_detail where order_id= #{id}")
        List<OrderDetail> getOrderDetailsByOrderId(Long id);

        /**
         * 根据状态统计订单数量
         * @param status
         */
        @Select("select count(id) from sky_take_out.orders where status = #{status}")
        Integer countStatus(Integer status);

        /**
         * 根据状态和下单时间查询订单
         * @param status
         * @param orderTime
         */
        @Select("select * from sky_take_out.orders where status = #{status} and order_time < #{orderTime}")
        List<Orders> getByStatusAndOrdertimeLT(Integer status, LocalDateTime orderTime);
}
